namespace OpenComposite.Inbox
{
	using System;
	using System.Collections;
	using System.ComponentModel;
	using System.Drawing;
	using System.Windows.Forms;

	using OpenComposite.Inbox.Data;

	/// <summary>
	/// Summary description for FormOptions.
	/// </summary>
	public class FormOptions : System.Windows.Forms.Form
	{
		#region Initialization

		/// <summary>
		/// Initialize frmOptions.
		/// </summary>
		public FormOptions()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		#endregion Initialization

		#region Protected Members

		#region Methods

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if ( disposing ) {
				if ( components != null ) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#endregion Methods

		#endregion Protected Members

		#region Private Members

		#region Fields

		private ComboBox cmbProtocol;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Label label1;
		private System.Windows.Forms.Label label2;
		private Label label3;
		private Label label4;
		private Label label5;
		private NumericUpDown numPort;
		private ToolStripButton saveToolStripButton;
		private System.Windows.Forms.TabControl tabCtrlMain;
		private System.Windows.Forms.TabPage tabServers;
		private ToolStrip toolStrip1;
		private TextBox txtAttachmentSvr;
		private System.Windows.Forms.TextBox txtDataSvr;
		private TextBox txtServer;

		#endregion Fields

		#region Event Handlers

		private void cmbProtocol_SelectedIndexChanged(object sender, EventArgs e)
		{
			updateUris();
		}

		private void frmOptions_Load(object sender, System.EventArgs e)
		{
			loadOptions();
		}

		private void numPort_ValueChanged(object sender, EventArgs e)
		{
			updateUris();
		}

		private void tbtnSaveExit_Click(object sender, EventArgs e)
		{
			if ( saveOptions() ) this.Close();
		}

		private void txt_TextChanged(object sender, EventArgs e)
		{
			updateUris();
		}

		#endregion Event Handlers

		#region Methods

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tabCtrlMain = new System.Windows.Forms.TabControl();
			this.tabServers = new System.Windows.Forms.TabPage();
			this.cmbProtocol = new System.Windows.Forms.ComboBox();
			this.numPort = new System.Windows.Forms.NumericUpDown();
			this.txtServer = new System.Windows.Forms.TextBox();
			this.txtDataSvr = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.txtAttachmentSvr = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.toolStrip1 = new System.Windows.Forms.ToolStrip();
			this.saveToolStripButton = new System.Windows.Forms.ToolStripButton();
			this.tabCtrlMain.SuspendLayout();
			this.tabServers.SuspendLayout();
			( (System.ComponentModel.ISupportInitialize)( this.numPort ) ).BeginInit();
			this.toolStrip1.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabCtrlMain
			// 
			this.tabCtrlMain.Controls.Add(this.tabServers);
			this.tabCtrlMain.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabCtrlMain.Location = new System.Drawing.Point(0, 25);
			this.tabCtrlMain.Name = "tabCtrlMain";
			this.tabCtrlMain.SelectedIndex = 0;
			this.tabCtrlMain.Size = new System.Drawing.Size(482, 204);
			this.tabCtrlMain.TabIndex = 0;
			// 
			// tabServers
			// 
			this.tabServers.Controls.Add(this.cmbProtocol);
			this.tabServers.Controls.Add(this.numPort);
			this.tabServers.Controls.Add(this.txtServer);
			this.tabServers.Controls.Add(this.txtDataSvr);
			this.tabServers.Controls.Add(this.label4);
			this.tabServers.Controls.Add(this.label5);
			this.tabServers.Controls.Add(this.label3);
			this.tabServers.Controls.Add(this.label2);
			this.tabServers.Controls.Add(this.txtAttachmentSvr);
			this.tabServers.Controls.Add(this.label1);
			this.tabServers.Location = new System.Drawing.Point(4, 22);
			this.tabServers.Name = "tabServers";
			this.tabServers.Size = new System.Drawing.Size(474, 178);
			this.tabServers.TabIndex = 0;
			this.tabServers.Text = "Services";
			this.tabServers.UseVisualStyleBackColor = true;
			// 
			// cmbProtocol
			// 
			this.cmbProtocol.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbProtocol.FormattingEnabled = true;
			this.cmbProtocol.Items.AddRange(new object[] {
            "http",
            "net.tcp"});
			this.cmbProtocol.Location = new System.Drawing.Point(117, 11);
			this.cmbProtocol.Name = "cmbProtocol";
			this.cmbProtocol.Size = new System.Drawing.Size(349, 21);
			this.cmbProtocol.TabIndex = 3;
			this.cmbProtocol.SelectedIndexChanged += new System.EventHandler(this.cmbProtocol_SelectedIndexChanged);
			// 
			// numPort
			// 
			this.numPort.Location = new System.Drawing.Point(117, 64);
			this.numPort.Maximum = new decimal(new int[] {
            65535,
            0,
            0,
            0});
			this.numPort.Name = "numPort";
			this.numPort.Size = new System.Drawing.Size(349, 20);
			this.numPort.TabIndex = 2;
			this.numPort.Value = new decimal(new int[] {
            30000,
            0,
            0,
            0});
			this.numPort.ValueChanged += new System.EventHandler(this.numPort_ValueChanged);
			// 
			// txtServer
			// 
			this.txtServer.Location = new System.Drawing.Point(117, 38);
			this.txtServer.Name = "txtServer";
			this.txtServer.Size = new System.Drawing.Size(349, 20);
			this.txtServer.TabIndex = 1;
			this.txtServer.Text = "Server";
			this.txtServer.TextChanged += new System.EventHandler(this.txt_TextChanged);
			// 
			// txtDataSvr
			// 
			this.txtDataSvr.Location = new System.Drawing.Point(117, 90);
			this.txtDataSvr.Name = "txtDataSvr";
			this.txtDataSvr.ReadOnly = true;
			this.txtDataSvr.Size = new System.Drawing.Size(349, 20);
			this.txtDataSvr.TabIndex = 1;
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(82, 67);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(29, 13);
			this.label4.TabIndex = 0;
			this.label4.Text = "Port:";
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(62, 14);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(49, 13);
			this.label5.TabIndex = 0;
			this.label5.Text = "Protocol:";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(70, 41);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(41, 13);
			this.label3.TabIndex = 0;
			this.label3.Text = "Server:";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(39, 93);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 13);
			this.label2.TabIndex = 0;
			this.label2.Text = "Data Service:";
			// 
			// txtAttachmentSvr
			// 
			this.txtAttachmentSvr.Location = new System.Drawing.Point(117, 116);
			this.txtAttachmentSvr.Name = "txtAttachmentSvr";
			this.txtAttachmentSvr.ReadOnly = true;
			this.txtAttachmentSvr.Size = new System.Drawing.Size(349, 20);
			this.txtAttachmentSvr.TabIndex = 1;
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(8, 119);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(103, 13);
			this.label1.TabIndex = 0;
			this.label1.Text = "Attachment Service:";
			// 
			// toolStrip1
			// 
			this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveToolStripButton});
			this.toolStrip1.Location = new System.Drawing.Point(0, 0);
			this.toolStrip1.Name = "toolStrip1";
			this.toolStrip1.Size = new System.Drawing.Size(482, 25);
			this.toolStrip1.TabIndex = 0;
			this.toolStrip1.Text = "toolStrip1";
			// 
			// saveToolStripButton
			// 
			this.saveToolStripButton.Image = global::OpenComposite.Inbox.Properties.Resources.png_save;
			this.saveToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
			this.saveToolStripButton.Name = "saveToolStripButton";
			this.saveToolStripButton.Size = new System.Drawing.Size(85, 22);
			this.saveToolStripButton.Text = "&Save && Exit";
			this.saveToolStripButton.Click += new System.EventHandler(this.tbtnSaveExit_Click);
			// 
			// frmOptions
			// 
			this.ClientSize = new System.Drawing.Size(482, 229);
			this.Controls.Add(this.tabCtrlMain);
			this.Controls.Add(this.toolStrip1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmOptions";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Optionen";
			this.Load += new System.EventHandler(this.frmOptions_Load);
			this.tabCtrlMain.ResumeLayout(false);
			this.tabServers.ResumeLayout(false);
			this.tabServers.PerformLayout();
			( (System.ComponentModel.ISupportInitialize)( this.numPort ) ).EndInit();
			this.toolStrip1.ResumeLayout(false);
			this.toolStrip1.PerformLayout();
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		private void loadOptions()
		{
			txtDataSvr.Text = Global.DataAccess.DataServiceUrl;
			txtAttachmentSvr.Text = Global.DataAccess.AttachmentServiceUrl;

			if ( !string.IsNullOrEmpty(Global.DataAccess.DataServiceUrl) ) {
				Uri uri = new Uri(Global.DataAccess.DataServiceUrl);
				cmbProtocol.SelectedItem = uri.Scheme.ToLower();
				txtServer.Text = uri.Host;
				numPort.Value = uri.Port;
			} else {
				cmbProtocol.SelectedItem = "net.tcp";
				txtServer.Text = "localhost";
				numPort.Value = 30000;
			}
		}

		private bool saveOptions()
		{
			bool bOK = true;
			string str = "";

			// Data Service
			str = txtDataSvr.Text;
			if ( str.Length == 0 || !Uri.IsWellFormedUriString(str, UriKind.Absolute) ) {
				MessageBox.Show("Please enter an url for the data service.");
				txtDataSvr.Focus();
				return false;
			}
			Global.DataAccess.DataServiceUrl = str;

			// Attachment Service
			str = txtAttachmentSvr.Text;
			if ( str.Length == 0 || !Uri.IsWellFormedUriString(str, UriKind.Absolute) ) {
				MessageBox.Show("Please enter an url for the attachment service.");
				txtAttachmentSvr.Focus();
				return false;
			}
			Global.DataAccess.AttachmentServiceUrl = str;

			return bOK;
		}

		private void updateUris()
		{
			string urldata = "<TYPE>://<SERVER>:<PORT>/opencompositeservices/data";
			string urlattachments = "<TYPE>://<SERVER>:<PORT>/opencompositeservices/attachments";
			string type = cmbProtocol.SelectedItem.ToString();
			string server = txtServer.Text;
			string port = numPort.Value.ToString();

			txtDataSvr.Text = urldata
				.Replace("<TYPE>", type)
				.Replace("<SERVER>", server)
				.Replace("<PORT>", port);
			txtAttachmentSvr.Text = urlattachments
				.Replace("<TYPE>", type)
				.Replace("<SERVER>", server)
				.Replace("<PORT>", port);
		}

		#endregion Methods

		#endregion Private Members
	}
}